﻿' •————————————————————————————————————————————————————————————————————————————————————————————————————————————•
' |                                                                                                            |
' |    GeoLock is a proof of concept (PoC). GeoLock uses Geosense for Windows,                                 |
' |    Microsoft Windows 7 Sensor and Location Interop Sample Library, Microsoft WindowsAPICodePack            |
' |    and Microsoft Bing Maps SDK to demonstrate how to retrieve geolocation data without                     |
' |    Global Positioning System (GPS) hardware.                                                               |
' |                                                                                                            |
' |    The GeoLocK application was written in VB.NET (WPF). The COM Interop Windows7.SensorAndLocation.dll,    |
' |    Microsoft.WindowsAPICodePack.dll and Microsoft.Maps.MapControl.WPF.dll supplied                         |
' |    with this application are x86 binaries.                                                                 |
' |                                                                                                            |
' |    Copyright (C) 2012 Pascal Hubert aka Néo <http://geolock.codeplex.com/>                                 |
' |                                                                                                            |
' |    Project administrator : Pascal Hubert aka Néo (neojudgment@hotmail.com)                                 |
' |                                                                                                            |
' |    This program is under Microsoft Public License (Ms-PL)                                                  |
' |                                                                                                            |
' |    You should have received a copy of the Microsoft Public License (Ms-PL)                                 |
' |    along with this program.  If not, see <http://msdn.microsoft.com/en-us/library/ff647936.aspx>.          |
' |                                                                                                            |
' •————————————————————————————————————————————————————————————————————————————————————————————————————————————•
Imports System.Runtime.InteropServices
Imports System.Reflection

Public Class About
    <DllImport("kernel32.dll")>
    Private Shared Function GetCurrentProcess() As IntPtr
    End Function

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto)>
    Private Shared Function GetModuleHandle(ByVal moduleName As String) As IntPtr
    End Function

    <DllImport("kernel32", CharSet:=CharSet.Auto, SetLastError:=True)>
    Private Shared Function GetProcAddress(ByVal hModule As IntPtr, <MarshalAs(UnmanagedType.LPStr)> ByVal procName As String) As IntPtr
    End Function

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Private Shared Function IsWow64Process(ByVal hProcess As IntPtr, <Runtime.InteropServices.Out()> ByRef wow64Process As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    Public Sub New()

        InitializeComponent()

        Top = Application.Current.MainWindow.Top
        Left = Application.Current.MainWindow.Left

        ' La version de GeoLock
        Dim maVersion As Version = Assembly.GetExecutingAssembly.GetName.Version

        ' Architecture 32 ou 64 bits
        Is64BitOperatingSystem()
        Dim bitOs As String
        If Is64BitOperatingSystem() Then
            bitOs = "64-bit"
        Else
            bitOs = "32-bit"
        End If

        LabelVersion.Content = "Version " & maVersion.ToString() & " " & bitOs & " Edition (alpha)"
    End Sub

    Private Shared Function Is64BitOperatingSystem() As Boolean

        ' Les programmes 64-bit démarrent seulement sous Win64
        If IntPtr.Size = 8 Then
            Return True
        Else
            ' Les programmes 32-bits démarrent sous Windows 32 et 64 bits
            ' On regarde si le processus courant est en 32 bits
            ' tournant sous un Windows 64 bits
            Dim flag As Boolean
            Return ((DoesWin32MethodExist("kernel32.dll", "IsWow64Process") AndAlso IsWow64Process(GetCurrentProcess(), flag)) AndAlso flag)
        End If
    End Function

    Private Shared Function DoesWin32MethodExist(ByVal moduleName As String, ByVal methodName As String) As Boolean

        Dim moduleHandle As IntPtr = GetModuleHandle(moduleName)
        If moduleHandle = IntPtr.Zero Then
            Return False
        End If
        Return (GetProcAddress(moduleHandle, methodName) <> IntPtr.Zero)
    End Function

    Private Sub AboutClosing(sender As Object, e As ComponentModel.CancelEventArgs) Handles Me.Closing

        MainWindow.FormTop = Top
        MainWindow.FormLeft = Left
    End Sub

    Private Sub AboutLoaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded

        Activate()

        Application.Current.MainWindow.Left = SystemParameters.PrimaryScreenWidth - 5000
        Application.Current.MainWindow.Top = SystemParameters.PrimaryScreenHeight - 5000
    End Sub

    Private Sub AboutLocationChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged

        MainWindow.MyWindowMove = True
    End Sub
End Class
